# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=shared-mime-info
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.4
pkgrel=1
pkgdesc="Freedesktop.org Shared MIME Info (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-glib2")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-itstool"
             "${MINGW_PACKAGE_PREFIX}-gettext"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "docbook-xsl"
             "xmlto")
install=${_realname}-${MSYSTEM}.install
url="https://freedesktop.org/Software/shared-mime-info"
msys2_repository_url="https://gitlab.freedesktop.org/xdg/shared-mime-info"
source=(https://gitlab.freedesktop.org/xdg/shared-mime-info/-/archive/${pkgver}/shared-mime-info-${pkgver}.tar.bz2
        0001-shared-mime-meson-install-manpage.patch
        program.manifest
        update-mime-database.hook.in)
sha256sums=('32dc32ae39ff1c1bf8434dd3b36770b48538a1772bc0298509d034f057005992'
            '8582cc8bd411d10092fe977f25ea5c21654b8edbec60a03b470c384784ff69fe'
            'f93191ad3e217ef9939cf16885ffe50ce920a0b639e183e46415f10df4b4db72'
            'a3f577d2bcfab00f15ae770bc9808953007488498e2d5a25e5f1be3a2479d29e')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-shared-mime-meson-install-manpage.patch"
}

build() {
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    -Dupdate-mimedb=false \
    "build-${MSYSTEM}" \
    "${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}"
}

package() {
  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}" --destdir="${pkgdir}"

  [[ ${CARCH} == i686 ]] && {
    _arch=x86
  } || {
    _arch=amd64
  }
  install -Dm644 "${srcdir}"/program.manifest "${pkgdir}"${MINGW_PREFIX}/bin/update-mime-database.exe.manifest
  sed -e "s|%PROGNAME%|GNU.update-mime-database|g" \
      -e "s|%ARCH%|${_arch}|g" \
      -i "${pkgdir}"${MINGW_PREFIX}/bin/update-mime-database.exe.manifest

  for hook in update-mime-database; do
    local hook_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-${hook}.hook";
    cp "${srcdir}/${hook}.hook.in" "${hook_path}"
    sed -s "s|@MINGW_HOOK_TARGET_PREFIX@|${MINGW_PREFIX:1}|g" -i "${hook_path}"
    sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${hook_path}"
    install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 "${hook_path}"
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
